//
//  UserNameViewController.m
//  CarCare
//
//  Created by ileo on 14-8-19.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "UserNameViewController.h"

@interface UserNameViewController ()

@property (nonatomic, strong) UITextField *nameTextField;

@end

@implementation UserNameViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = LOCSTRING(@"name");
    __weak __typeof(self) wself = self;
    [self navSetupLeftTitle:@"取消" action:^{
        [wself.view endEditing:YES];
        [wself dismissViewControllerAnimated:YES completion:nil success:NO info:nil];
    }];
    [self navSetupRightTitle:@"保存" action:^{
        [wself.view endEditing:YES];
        if (wself.nameTextField.text.length > 20) {
            [AlertMgr showShortNormalTextTips:@[@"用户名过长,建议不要超过20个字符"]];
        }else{
            if ([Check isNoNull:wself.nameTextField.text]) {
                [[[NetUser setUserName:wself.nameTextField.text] requestWithLoad:RequestLoadShowLoading|RequestLoadShowErrorTips] requestSuccess:^(id JSON) {
                    USERINFO.userName = wself.nameTextField.text;
                    TipsView *tips = [TipsView okTipsWithTitle:nil lightTitles:@[LOCSTRING(@"set_name_succ")]];
                    [tips addOKButton:LOCSTRING(@"ok") clickOK:^{
                        [AlertMgr dismiss:TYPE_VIEW_ALERT];
                        [wself dismissViewControllerAnimated:YES completion:nil success:YES info:nil];
                    }];
                    [AlertMgr showView:tips];
                }];
            }
        }
    }];
    
    self.nameTextField = [self textFieldWithFrame:FactoryRectDefault(TOP_SIDE) type:TEXTFIELD_Name];

    if ([USERINFO.userName isNotNil]) {
        self.nameTextField.text = USERINFO.userName;
    }
    
    // Do any additional setup after loading the view.
}
- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.nameTextField) {
        return 20;
    }
    else{
        return [super textFieldMaxNumberWithTextField:textField];
    }
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.nameTextField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
